const mobileScreen = window.matchMedia("(max-width: 990px )");
var menubtn = document.getElementById("menubtn");
var togbtn = document.getElementById("togbtn");

togbtn.addEventListener("click", function () {
  menubtn.click();
});
$(document).ready(function () {
  $(".dashboard-nav-dropdown-toggle").click(function () {
    $(this)
      .closest(".dashboard-nav-dropdown")
      .toggleClass("show")
      .find(".dashboard-nav-dropdown")
      .removeClass("show");
    $(this).parent().siblings().removeClass("show");
  });
  $(".menu-toggle").click(function () {
    if (mobileScreen.matches) {
      $(".dashboard-nav").toggleClass("mobile-show");
    } else {
      $(".dashboard").toggleClass("dashboard-compact");
    }
  });
});
//echarts
var chartDom = document.getElementById("main");
var myChart = echarts.init(chartDom);
var show_data = [];
setInterval(function () {
  echartData();
}, 15000);
//进入页面立即执行
echartData();
function echartData() {
  fetch(`${apirUrl}/api/getdata`)
    .then((res) => res.json())
    .then((data) => {
      // console.log(data);
      var option = {
        title: {
          text: "后台数据柱状图",
          subtext: "数据来自网络",
          left: "center",
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },

        xAxis: {
          type: "category",
          data: [
            "新闻总浏览量",
            "评论总点赞数",
            "注册用户数",
            "评论总数",
            "新闻数量",
          ],
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            data: [
              data.allPageViews,
              data.allVoteNum,
              data.userNum,
              data.commentNum,
              data.newsNum,
            ],
            type: "bar",
            showBackground: true,
            backgroundStyle: {
              color: "rgba(180, 180, 180, 0.2)",
            },
          },
        ],
      };

      option && myChart.setOption(option);
    });
}
